package com.network.service.impl;


import com.network.mapper.PhoneMapper;
import com.network.mapper.UserInfoMapper;
import com.network.mapper.UserRelationMapper;
import com.network.pojo.UserInfo;
import com.network.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserInfoServiceImpl implements UserInfoService {


    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    UserRelationMapper userRelationMapper;

    @Autowired
    PhoneMapper phoneMapper;


    @Override
    public List<UserInfo> getUserInfoByKey(String name, Date date) {
        List<UserInfo> userInfoByKey = userInfoMapper.getUserInfoByKey(name, date);
        for (UserInfo userInfo : userInfoByKey) {
            userInfo.getPhones();
            System.out.println(userInfo);
        }
        return userInfoByKey;
    }

    @Override
    public int countUserByName(Date date, String name) {
        return userInfoMapper.countUserByName(date,name);
    }

    @Override
    public int countUser(Date date) {
        return userInfoMapper.countUser(date);
    }

    @Transactional(rollbackFor = Exception.class,isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public List<UserInfo> getUserInfoByName(String name,Date date) {

        List<UserInfo> userInfoByName = userInfoMapper.getUserInfoByName(name,date);
        for (UserInfo userInfo : userInfoByName) {
            userInfo.getPhones();
        }
        return userInfoByName;
    }

    @Override
    public List<UserInfo> getAllUserInfo(Date date) {
        List<UserInfo> allUserInfo = userInfoMapper.getAllUserInfo(date);

        for (UserInfo userInfo : allUserInfo) {
            userInfo.getPhones();
        }

        return allUserInfo;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    @Override
    public UserInfo findUserInfoById(int id) {
        UserInfo userInfo = userInfoMapper.findUserInfoById(id);
        if(userInfo != null) {
            userInfo.getPhones();
        }
        return userInfo;
    }

    /**
     * 方法之中要修改与用户手机号有关的
     * @param userInfo
     */
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    @Override
    public void updateUserInfo(UserInfo userInfo) {
        userInfoMapper.updateUserInfo(userInfo);
        phoneMapper.deletePhoneNumberByUid(userInfo.getId());
        phoneMapper.insert(userInfo.getPhones(),userInfo.getId());
    }


    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ)
    @Override
    public UserInfo findUserInfoByUsername(String username) {

        UserInfo userInfo = userRelationMapper.finUserInfoByUsername(username);
        if(userInfo != null) {
            userInfo.getPhones();
        }
        return userInfo;
    }
}
